package org.yzh.web.endpoint;

import io.github.yezhihao.netmc.session.Session;
import io.github.yezhihao.netmc.session.SessionListener;
import org.eclipse.jetty.util.ajax.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.yzh.web.mapper.DeviceMapper;
import org.yzh.web.model.entity.DeviceDO;
import org.yzh.web.model.enums.SessionKey;
import org.yzh.web.model.vo.DeviceInfo;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class JTSessionListener implements SessionListener {

    @Autowired
    private DeviceMapper deviceMapper;

    public static final List<Object> DVS = new ArrayList<>();
    @Override
    public void sessionCreated(Session session) {
        DVS.add(session);
        System.out.println(session);
        System.out.println(session.getClientId());
    }

    @Override
    public void sessionDestroyed(Session session) {
        DVS.remove(session);
        System.out.println("离线");
        DeviceInfo device = (DeviceInfo)session.getAttribute(SessionKey.DeviceInfo);
        if(device != null)
            deviceMapper.update(new DeviceDO(device.getDeviceId(), false, LocalDateTime.now()));
    }
}